package com.yinhe.music.yhmusic.radio;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.RadioClassifyAdapter;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.model.RadioClassify;
import com.yinhe.music.yhmusic.network.NetworkManager;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;
import com.yinhe.music.yhmusic.network.schedulers.SchedulerProvider;
import com.yinhe.music.yhmusic.widget.CustomLoadMoreView;

import java.util.List;

import butterknife.BindView;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * 电台类别
 *
 * @author 何晓华
 * @date 2017/9/29
 */

public class RadioClassifyActivity extends BaseServiceActivity {
    @BindView(R.id.recommend_music_recyclerview)
    RecyclerView recyclerview;
    private RadioClassifyAdapter mAdapter;
    private int mPageNum;

    CompositeDisposable mDisposable = new CompositeDisposable();

    @Override
    public void initView() {
        initData();
        initAdapter();
        reqClassifyList();
        showQuickControl(true);
        setToolbar("电台分类");
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_collection_radio;
    }

    @Override
    public BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDisposable.dispose();
    }

    private void initData() {
        mPage = 1;
        mPageSize = 10;
    }

    private void initAdapter() {
        mAdapter = new RadioClassifyAdapter();

        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            RadioClassify classify = (RadioClassify) adapter.getData().get(position);
            RxBus.get().post(RxBusEventType.Radio.UPDATE_RADIO_LIST, classify);
            finish();
        });

        mAdapter.setPreLoadNumber(2);
        mAdapter.setLoadMoreView(new CustomLoadMoreView());

        mAdapter.setOnLoadMoreListener(() -> {
            if (mPage <= mPageNum) {
                reqClassifyList();
            } else {
                mAdapter.loadMoreEnd();
            }
        }, recyclerview);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);

        recyclerview.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerview.setAdapter(mAdapter);
    }

    private void reqClassifyList() {
        Disposable collectDisposable = NetworkManager.getWebService().getRadioClassify(mPageSize, mPage)
                .compose(ResponseTransformer.handleResult())
                .compose(SchedulerProvider.getInstance().applySchedulers())
                .subscribe(radioClassify -> {
                    setClassifyList(radioClassify);
                }, throwable -> {
                    showMessage(throwable);
                });

        mDisposable.add(collectDisposable);
    }

    private void setClassifyList(RadioClassify data) {
        List<RadioClassify> list = data.getRadioClassifyList();
        if (list == null) {
            return;
        }
        mPageNum = data.getPageNum();
        setLoadMoreList(list, mPageNum, mAdapter);
    }
}
